<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="!dataForm.id ? '新增' : '修改'"
    :visible.sync="visible"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="120px"
      @keyup.enter.native="dataFormSubmit()"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" placeholder="用户名" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="dataForm.password" placeholder="密码" />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号码" />
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <single-upload v-model="dataForm.avatar" placeholder="头像" />
      </el-form-item>
      <el-form-item label="代理等级" prop="levelId">
        <el-select v-model="dataForm.levelId" clearable placeholder="请选择">
          <el-option
            v-for="item in memberLevelList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="启用状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <!--      <el-form-item label="注册时间" prop="createTime">-->
      <!--        <el-input v-model="dataForm.createTime" placeholder="注册时间" />-->
      <!--      </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-has-permission="['member:member:edit','member:member:add']" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addMember, getMember, updateMember } from '@/api/ums/member'
import SingleUpload from '@/components/Upload/SingleUpload'
import checkPermission from '@/utils/permission'
import { listMember_level } from '@/api/ums/member_level'

export default {
  components: { SingleUpload },
  data() {
    return {
      visible: false,
      dataForm: {
        id: undefined,
        levelId: undefined,
        parentId: undefined,
        username: '',
        password: '',
        mobile: '',
        avatar: '',
        status: 1
      },
      memberLevelList: [],
      dataRule: {
        levelId: [
          { required: true, message: '会员等级id不能为空', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '启用状态不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    checkPermission,
    init(id) {
      this.listMemberLevel()
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getMember(this.dataForm.id).then(({ data }) => {
            this.dataForm = data
            this.dataForm.username = data.nickname
            this.dataForm.password = ''
          })
        }
      })
    },
    listMemberLevel() {
      listMember_level({ status: 1, queryMode: 'list' }).then(({ data }) => {
        this.memberLevelList = data
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (!this.dataForm.id) {
            addMember(this.dataForm).then(({ data }) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            updateMember(this.dataForm).then(({ data }) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    }
  }
}
</script>
